#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

/*  
1  ./write 1.txt
2  argc>=3
3  argv[0]="./write"
4  argv[1]="1.txt"
5  argv[2]="string1"
6  argv[3]="string2"...
*/

int main (int argc, char **argv)
{
    int fd;
    int i;
    int iRet;
    if(argc < 3) 
    {
        printf("Usage: %s<filename><string1><string2>...\n", argv[0]);
        return -1;
    }

    /*
    以可读可写的方式打开文件，
    如果文件不存在则创建文件，
    */

    fd = open(argv[1], O_RDWR | O_CREAT ,0666);
    if(fd == -1)
    {
        printf("Error number: %d\n", errno);//打印错误码
        printf("Error message: %s\n", strerror(errno));//把错误码转换成错误信息打印出来    
        perror("open");//打印错误信息
        return -1;
    }
    else
    {
        printf("fd=%d\n", fd);
    }
    for(i=2;i<=argc;i++)
    {
       iRet = write(fd, argv[i],strlen(argv[i]));
       if(iRet != strlen(argv[i]))
       {
        perror("write()");
       }       
        write(fd,"\r\n",2);
    }
    iRet = lseek(fd, 5, SEEK_SET);
    write (fd,"iamin",5);
    printf("lseek%d",iRet);

    while(1)
    {
        sleep(10);
    }
    close(fd);//关闭
    return 0;
}